/* 前端使用AES加密 */
import CryptoJS from 'crypto-js'

export default function encrypt(plaintText, uuid) {
  const CRYPTOJSKEY = window.APP_CONFIG && window.APP_CONFIG.CRYPTOJSKEY
  if (CRYPTOJSKEY) {
    const options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }
    if (!uuid) {
      uuid = Math.random().toString(36)
    }
    uuid = uuid.substr(2, 4)
    plaintText += uuid.toString() // 随机拼接4位再加密
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
    const encryptedData = CryptoJS.AES.encrypt(plaintText, key, options)
    return encryptedData.toString()
    /* let encryptedBase64Str = encryptedData.toString().replace(/\//g, '_')
    encryptedBase64Str = encryptedBase64Str.replace(/\+/g, '-')
    return encryptedBase64Str */
  }
  return plaintText
}
